package framework.sinkers;

import framework.DataSink;
import java.util.Hashtable;

/**
 * Sink that outputs to the console
 */
public class StdOutSink extends DataSink {


    private int[] idsToPrint;
    private MeasureFormatter formatter;

    /**
     * 
     * @param idsToPrint The list of ids that will be handled in each frame.
     * @param formatter The formatter function, that will handle the list of values to be printed.
     */
    public StdOutSink(int[] idsToPrint, MeasureFormatter formatter) {
        this.formatter = formatter;
        this.idsToPrint = idsToPrint;
    }

    /**
     *
     * @param hash List of measurements taken for each ID.
     */
    @Override
    protected void write(Hashtable<Integer, Long> hash) {

        long extrapolMask = 0;
        // get the extrapolated values mask
        Long measure = hash.remove(6);
        if (measure != null) {
            extrapolMask = measure;
        }

        for (int id : this.idsToPrint) {
            measure = hash.get(id);
            if (measure != null) {

                // check if this is an extrapoled value (add * in the formatter for instance)
                boolean extrapol = (((extrapolMask >> id) & 1) == 1);

                System.out.print(String.format("%-" + 20 + "s", formatter.format(id, measure, extrapol)));
            }
        }
        System.out.println();
    }

    /**
     * Closing taks, nothing in this case
     */
    @Override
    protected void finish() {
        // do nothing
    }

}
